package visitor;

import operators.Cat;
import operators.Minus;
import operators.Plus;
import operators.Produs;
import arbore.Constanta;
import arbore.Necunoscuta;
import arbore.Nod;
import functions.Cos;
import functions.Sin;

public class DerivareVisitor extends Visitor{
	
	private String s;
	private ToStringVisitor tsv=new ToStringVisitor();
	
	public DerivareVisitor(){
		s="";
	}
	public DerivareVisitor(String s) {
		super();
		this.s = s;
	}

	public String getS() {
		return s;
	}

	public void setS(String s) {
		this.s = s;
	}

	public void visit(Plus p) {
		if(p.getfStang()!=null && p.getfDrept()!=null){
			visit(p.getfStang());
			String s1=this.s;
			visit(p.getfDrept());
			String s2=this.s;
			s=p.concatTermeni(s1,s2);
		}
	}

	public void visit(Minus m) {
		if(m.getfStang()!=null && m.getfDrept()!=null){
			visit(m.getfStang());
			String s1=this.s;
			visit(m.getfDrept());
			String s2=this.s;
			s=m.concatTermeni(s1,s2);
		}
	}

	public void visit(Cat p) {
		if(p.getfStang()!=null && p.getfDrept()!=null){
			visit(p.getfStang());
			String s1=this.s;
			visit(p.getfDrept());
			String s2=this.s;
			
			p.getfStang().acceptVisitor(tsv);
			String f1=this.getS();
			p.getfDrept().acceptVisitor(tsv);
			String f2=this.getS();
			this.s=Cat.concatTermens(Minus.concatTermens(Produs.concatTermens(s1,f2),Produs.concatTermens(f1, s2)),Produs.concatTermens(f2, f2));
		}
	}

	public void visit(Produs p) {
		if(p.getfStang()!=null && p.getfDrept()!=null){
			visit(p.getfStang());
			String s1=this.s;
			visit(p.getfDrept());
			String s2=this.s;
			
			p.getfStang().acceptVisitor(tsv);
			String f1=this.getS();
			p.getfDrept().acceptVisitor(tsv);
			String f2=this.getS();
			this.s=Plus.concatTermens(Produs.concatTermens(s1, f2),Produs.concatTermens(f1, s2));
		}
	}

	public void visit(Necunoscuta n) {
		this.s="1";
	}

	public void visit(Constanta c) {
		this.s="0";
	}

	public void visit(Sin s) {
		if(s.getfStang()!=null){
			visit(s.getfStang());
			String fsd=this.s;
			s.getfStang().acceptVisitor(tsv);
			String fs=tsv.getS();
			this.s=Produs.concatTermens(Cos.concatTermens(fs),fsd);
		}
	}

	public void visit(Cos c) {
		if(c.getfStang()!=null){
			visit(c.getfStang());
			String fsd=this.s;
			c.getfStang().acceptVisitor(tsv);
			String fs=tsv.getS();
			this.s="-"+Produs.concatTermens(fsd,Sin.concatTermens(fs));
		}
	}

	public void visit(Nod n) {
		
	}

}
